home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / checkbox / job.py < prev    next >
Text File  |  2009-11-05  |  3KB  |  107 lines

  1. #
  2. # This file is part of Checkbox.
  3. #
  4. # Copyright 2008 Canonical Ltd.
  5. #
  6. # Checkbox is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU General Public License as published by
  8. # the Free Software Foundation, either version 3 of the License, or
  9. # (at your option) any later version.
  10. #
  11. # Checkbox is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. # GNU General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU General Public License
  17. # along with Checkbox.  If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. import os
  20. import logging
  21.  
  22. from gettext import gettext as _
  23. from string import Template
  24.  
  25. from checkbox.lib.iterator import IteratorContain
  26. from checkbox.lib.process import Process
  27. from checkbox.lib.signal import signal_to_name, signal_to_description
  28.  
  29. from checkbox.depends import DependsIterator
  30. from checkbox.frontend import frontend
  31. from checkbox.requires import RequiresIterator
  32.  
  33.  
  34. FAIL = "fail"
  35. PASS = "pass"
  36. UNINITIATED = "uninitiated"
  37. UNRESOLVED = "unresolved"
  38. UNSUPPORTED = "unsupported"
  39. UNTESTED = "untested"
  40.  
  41. ALL_STATUS = [FAIL, PASS, UNINITIATED, UNRESOLVED, UNSUPPORTED, UNTESTED]
  42.  
  43.  
  44. class Job(object):
  45.  
  46.     def __init__(self, command, environ=None, timeout=None, user=None):
  47.         if environ is None:
  48.             environ = []
  49.  
  50.         self.command = command
  51.         self.environ = environ
  52.         self.timeout = timeout
  53.         self.user = user
  54.  
  55.     @frontend("get_job_result")
  56.     def execute(self):
  57.         # Sanitize environment
  58.         process_environ = dict(os.environ)
  59.         for environ in self.environ:
  60.             key, value = environ.split("=", 1)
  61.             value = Template(value).safe_substitute(process_environ)
  62.             process_environ[key] = value
  63.  
  64.         logging.info("Running command: %s", self.command)
  65.         process = Process(self.command, process_environ)
  66.         if not process.read(self.timeout):
  67.             logging.info("Command timed out, killing process.")
  68.             process.kill()
  69.  
  70.         process_status = process.cleanup()
  71.         if os.WIFEXITED(process_status):
  72.             exit_status = os.WEXITSTATUS(process_status)
  73.             if exit_status == 0:
  74.                 status = PASS
  75.                 data = process.outdata
  76.                 if not data:
  77.                     data = process.errdata
  78.             elif exit_status == 127:
  79.                 status = UNRESOLVED
  80.                 data = _("Command not found.")
  81.             else:
  82.                 status = FAIL
  83.                 data = (process.errdata
  84.                              or process.outdata)
  85.         elif os.WIFSIGNALED(process_status):
  86.             status = UNRESOLVED
  87.             term_signal = os.WTERMSIG(process_status)
  88.             data = _("Command received signal %s: %s") % \
  89.                 (signal_to_name(term_signal),
  90.                  signal_to_description(term_signal))
  91.         else:
  92.             raise Exception, "Command not terminated: %s" \
  93.                 % self.command
  94.  
  95.         duration = process.endtime - process.starttime
  96.  
  97.         return (status, data, duration)
  98.  
  99.  
  100. class JobIterator(IteratorContain):
  101.  
  102.     def __init__(self, iterator, registry, compare=None):
  103.         iterator = RequiresIterator(iterator, registry)
  104.         iterator = DependsIterator(iterator, compare)
  105.  
  106.         super(JobIterator, self).__init__(iterator)
  107.